<?php


class Menuadmin extends CActiveRecord
{
	
	public function tableName()
	{
		return 'menuadmin';
	}
   
   
   public function primaryKey(){
      return 'MenuAdminID';
   }
	
  
	public function rules()
	{
		
		
		return array(
         
         array('MenuName', 'required',
               'message' => "{attribute} không được bỏ trống"),
         array('IsHome, IsActive','boolean'),
			array('IsActive, IsHome, Menu1, Menu2, Menu3, Menu4', 'numerical', 'integerOnly'=>true),
			array('MenuName', 'length', 'max'=>50),
			array('MenuPath, MenuIcon, MenuImage', 'length', 'max'=>200),
			array('MenuCode', 'length', 'max'=>100),
         array('MenuAdminID, MenuPath, MenuIcon, MenuImage, MenuCode', 'safe'),
			
			
			array('MenuAdminID, MenuName, MenuPath, MenuIcon, MenuImage, IsActive, IsHome, MenuCode, Menu1, Menu2, Menu3, Menu4', 'safe', 'on'=>'search'),
         
		);
	}

	
	public function relations()
	{
		
		
		return array(
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'MenuAdminID' => 'Menu Admin',
			'MenuName' => 'Tên menu',
			'MenuPath' => 'Đường dẫn',
			'MenuIcon' => 'Icon',
			'MenuImage' => 'Ảnh đại diện',
			'IsActive' => 'Kích hoạt',
			'IsHome' => 'Trang chủ',
			'MenuCode' => 'Mã menu',
			'Menu1' => 'Menu cấp 1',
			'Menu2' => 'Menu cấp 2',
			'Menu3' => 'Menu cấp 3',
			'Menu4' => 'Menu cấp 4',
		);
	}

	
	public function search()
	{
		

		$V0v5vsnfxezw=new CDbCriteria;

		$V0v5vsnfxezw->compare('MenuAdminID',$this->MenuAdminID,true);
		$V0v5vsnfxezw->compare('MenuName',$this->MenuName,true);
		$V0v5vsnfxezw->compare('MenuPath',$this->MenuPath,true);
		$V0v5vsnfxezw->compare('MenuIcon',$this->MenuIcon,true);
		$V0v5vsnfxezw->compare('MenuImage',$this->MenuImage,true);
		$V0v5vsnfxezw->compare('IsActive',$this->IsActive);
		$V0v5vsnfxezw->compare('IsHome',$this->IsHome);
		$V0v5vsnfxezw->compare('MenuCode',$this->MenuCode,true);
		$V0v5vsnfxezw->compare('Menu1',$this->Menu1);
		$V0v5vsnfxezw->compare('Menu2',$this->Menu2);
		$V0v5vsnfxezw->compare('Menu3',$this->Menu3);
		$V0v5vsnfxezw->compare('Menu4',$this->Menu4);

		return new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw,
		));
	}

	
	public static function model($Vgmajjibziuy=__CLASS__)
	{
		return parent::model($Vgmajjibziuy);
	}
   
   
   public function getMenuByLevel($Vbfcajnp3xwj = 0){
      $V0ayqlxdb1lr = array();
      $Vwmlpe2lzlsh = " SELECT * FROM " . $this->tableName() . " WHERE 1";
      $V24noxecbnll = '';
      if($Vbfcajnp3xwj > 0 && $Vbfcajnp3xwj < 4){
         switch ($Vbfcajnp3xwj){
            case 1 :
               $V24noxecbnll .= " AND Menu2 = 0";
               break;
            case 2 :
               $V24noxecbnll .= " AND Menu3 = 0";
               break;
            case 3 :
               $V24noxecbnll .= " AND Menu4 = 0";
         }
      }
      $Vwmlpe2lzlsh .= $V24noxecbnll;
      $Vzpv2tsfrg4l = $this->findAllBySql($Vwmlpe2lzlsh);
      foreach($Vzpv2tsfrg4l as $Vlqihxrrpwig){
         $V0ayqlxdb1lr[] = $Vlqihxrrpwig->attributes;
      }
      return $V0ayqlxdb1lr;
   }
   
   
   
   public function getLevel($V5as2qkxgfs4){    
      
      $Vwmlpe2lzlsh = "SELECT * FROM menuadmin WHERE MenuAdminID =:id";
      $Vjgnlv50f20c = Yii::app()->db;
      $Vyfkfneh1yzo = $Vjgnlv50f20c->createCommand($Vwmlpe2lzlsh);
      $Vyfkfneh1yzo->bindParam(":id",$V5as2qkxgfs4);
      $Vcp5zer14q1b = $Vyfkfneh1yzo->queryRow();
      $this->attributes = $Vcp5zer14q1b;
      
      
      if($this->Menu4 != 0)
         $Vbfcajnp3xwj = 4;
      else if($this->Menu3 != 0)
         $Vbfcajnp3xwj = 3;
      else if($this->Menu2 != 0)
         $Vbfcajnp3xwj = 2;
      else 
         $Vbfcajnp3xwj = 1;
      return $Vbfcajnp3xwj;
   }
   
   
   public function getParent($V5as2qkxgfs4){
      $V0ayqlxdb1lr = array();
      $Vbfcajnp3xwj = $this->getLevel($V5as2qkxgfs4);
      $Vwmlpe2lzlsh = "SELECT
            	a.*
            FROM
            	menuadmin a
            INNER JOIN (
            	SELECT
            		MenuAdminID,
            		Menu1,
            		Menu2,
            		Menu3,
            		Menu4
            	FROM
            		menuadmin c
            	WHERE
            		MenuAdminID = $V5as2qkxgfs4";
      $Vev3x0lpfv03 = '';
      if($Vbfcajnp3xwj == 2){ 
         $Vev3x0lpfv03_1 = '( b.Menu1 = a.Menu1 
                        AND b.Menu1 > 0 
                        AND a.Menu2 = 0 )';
         $Vwmlpe2lzlsh_1 = ' ) b ON ' . $Vev3x0lpfv03 . $Vev3x0lpfv03_1 . ' WHERE a.IsActive = 1';
         $Vmkl4o0ujwfd = $this->findBySql($Vwmlpe2lzlsh.$Vwmlpe2lzlsh_1);
         $V0ayqlxdb1lr[1] = $Vmkl4o0ujwfd->attributes;
         return $V0ayqlxdb1lr;
      }
      if($Vbfcajnp3xwj == 3){ 
         $Vev3x0lpfv03_1 = '( b.Menu1 = a.Menu1 
                        AND b.Menu1 > 0 
                        AND a.Menu2 = 0 )';
         $Vwmlpe2lzlsh_1 = ' ) b ON ' . $Vev3x0lpfv03 . $Vev3x0lpfv03_1 . ' WHERE a.IsActive = 1';
         $Vmkl4o0ujwfd = $this->findBySql($Vwmlpe2lzlsh.$Vwmlpe2lzlsh_1);
         $V0ayqlxdb1lr[1] = $Vmkl4o0ujwfd->attributes;
         $Vev3x0lpfv03_2 = '( b.Menu1 = a.Menu1 
                        AND b.Menu2 = a.Menu2
                     	AND b.Menu1 > 0
                     	AND a.Menu2 > 0
                     	AND a.menu3 = 0 )';
         $Vwmlpe2lzlsh_2 = ' ) b ON ' . $Vev3x0lpfv03 . $Vev3x0lpfv03_2 . ' WHERE a.IsActive = 1';
         $Viqxomt0oubp = $this->findBySql($Vwmlpe2lzlsh.$Vwmlpe2lzlsh_2);
         $V0ayqlxdb1lr[2] = $Viqxomt0oubp->attributes;
      }
      return $V0ayqlxdb1lr;
      
   }
   
   
   public function getIdParentMax($V5as2qkxgfs4){
      $V0ayqlxdb1lr = 0;
      $Vwmlpe2lzlsh = "SELECT
               	a.*
               FROM
               	menuadmin a
               INNER JOIN (
               	SELECT
               		MenuAdminID,
               		Menu1,
               		Menu2,
               		Menu3,
               		Menu4
               	FROM
               		menuadmin c
               	WHERE
               		MenuAdminID = $V5as2qkxgfs4
               ) b ON (
               	b.Menu1 = a.Menu1
               	AND a.Menu2 = 0
               )
               WHERE
               	a.IsActive = 1
              ";
      $Vcp5zer14q1b = $this->findBySql($Vwmlpe2lzlsh);
      if(!is_null($Vcp5zer14q1b))
         $V0ayqlxdb1lr = $Vcp5zer14q1b->attributes['MenuAdminID'];
      return $V0ayqlxdb1lr;
   }
   
   
   public function getChild($V5as2qkxgfs4){
      $V0ayqlxdb1lr = array();
      
      $Vbfcajnp3xwj = $this->getLevel($V5as2qkxgfs4);
         $Vwmlpe2lzlsh = "SELECT
                  	a.*
                  FROM
                  	menuadmin a
                  INNER JOIN (
                  	SELECT
                  		MenuAdminID,
                  		Menu1,
                  		Menu2,
                  		Menu3,
                  		Menu4
                  	FROM
                  		menuadmin c
                  	WHERE
                  		MenuAdminID = $V5as2qkxgfs4
                  ) b ON ( ";
      if($Vbfcajnp3xwj == 1){
         
         $Vwmlpe2lzlsh_2 = "b.Menu1 = a.Menu1
	                 AND a.Menu3 = 0 )
                  WHERE
                  	a.IsActive = 1
                  AND	a.MenuAdminID != $V5as2qkxgfs4";
         $Vwhluwif3pr0 = $this->findAllBySql($Vwmlpe2lzlsh.$Vwmlpe2lzlsh_2);
         $Vukbe4ustnpk = array();
         foreach($Vwhluwif3pr0 as $Vlqihxrrpwig){
            $Vukbe4ustnpk[] = $Vlqihxrrpwig->attributes;
         }
         $V0ayqlxdb1lr[2] = $Vukbe4ustnpk;
         
         
         $Vwmlpe2lzlsh_3 = "b.Menu1 = a.Menu1
	                 AND a.Menu3 > 0 )
                  WHERE
                  	a.IsActive = 1
                  AND	a.MenuAdminID != $V5as2qkxgfs4";
         $Vwhluwif3pr0 = $this->findAllBySql($Vwmlpe2lzlsh.$Vwmlpe2lzlsh_3);
         $Vukbe4ustnpk = array();
         foreach($Vwhluwif3pr0 as $Vlqihxrrpwig){
            $Vukbe4ustnpk[] = $Vlqihxrrpwig->attributes;
         }
         $V0ayqlxdb1lr[3] = $Vukbe4ustnpk;
      }else if($Vbfcajnp3xwj == 2){
         
         $Vwmlpe2lzlsh_3 = "b.Menu1 = a.Menu1
	                 AND a.Menu3 > 0 )
                  WHERE
                  	a.IsActive = 1
                  AND	a.MenuAdminID != $V5as2qkxgfs4";
         $Vwhluwif3pr0 = $this->findAllBySql($Vwmlpe2lzlsh.$Vwmlpe2lzlsh_3);
         $Vukbe4ustnpk = array();
         foreach($Vwhluwif3pr0 as $Vlqihxrrpwig){
            $Vukbe4ustnpk[] = $Vlqihxrrpwig->attributes;
         }
         $V0ayqlxdb1lr[3] = $Vukbe4ustnpk;
      }
      
      return $V0ayqlxdb1lr;
      
   }
   
   
   public function getAllLevel1(){
      $Vwmlpe2lzlsh = "SELECT * FROM menuadmin WHERE IsActive = 1 AND Menu2 = 0";
      $V02c3ztfj4ey = array();
      $Vzpv2tsfrg4l = $this->findAllBySql($Vwmlpe2lzlsh);
      if(count($Vzpv2tsfrg4l) > 0){
         foreach($Vzpv2tsfrg4l as $Vlqihxrrpwig){
            $V02c3ztfj4ey[] = $Vlqihxrrpwig->attributes;
         }
      }
      return $V02c3ztfj4ey;
   }
}
